export function CheckSocialCreditCode(code: string) {
  let patrn = /^[0-9A-Z]+$/;
  //18位校验及大写校验
  if (code.length != 18 || patrn.test(code) == false) {
    return false;
  } else {
    let Ancode; //统一社会信用代码的每一个值
    let Ancodevalue; //统一社会信用代码每一个值的权重
    let total = 0;
    let weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28]; //加权因子
    let str = "0123456789ABCDEFGHJKLMNPQRTUWXY";
    //不用I、O、S、V、Z
    for (let i = 0; i < code.length - 1; i++) {
      Ancode = code.substring(i, i + 1);
      Ancodevalue = str.indexOf(Ancode);
      total = total + Ancodevalue * weightedfactors[i];
      //权重与加权因子相乘之和
    }
    let logiccheckcode: number | string = 31 - (total % 31);
    if (logiccheckcode == 31) {
      logiccheckcode = 0;
    }
    let Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";
    let Array_Str = Str.split(",");
    logiccheckcode = Array_Str[logiccheckcode];
    let checkcode = code.substring(17, 18);
    if (logiccheckcode != checkcode) {
      return false;
    }
    return true;
  }
}

/* 统一社会信用代码检查 */
export function SocialCreditCodeBlur(rule: any, value: any, callback: any) {
  if (!value || value === undefined || value === "") {
    callback(new Error("统一社会信用代码不能为空"));
    return;
  }
  if (value.length < 18) {
    callback(new Error("统一社会信用代码为18位"));
    return;
  } else {
    let check = CheckSocialCreditCode(value);
    if (check) {
      callback();
    } else {
      callback("请输入正确的统一社会信用代码");
    }
  }
}
